
ac.game:event '玩家-取消挑战'(function(trg,player)
    local unit = player:get_data('当前挑战怪')
    if unit then
        unit:remove()
        unit.effect:remove()
        player:notify('玩家-创建挑战怪',player)
    end
    local unit = player:get_data('专属Boss1')
    if unit then
        unit:remove()
        player:set_data('专属Boss1',nil)
    end
    local unit = player:get_data('专属Boss2')
    if unit then
        unit:remove()
        player:set_data('专属Boss2',nil)
    end
end)

ac.sync:event '取消挑战'(function(_,player)
    player:notify('玩家-取消挑战',player)
end)

ac.game:event '玩家-创建挑战怪'(function(trg,player)
    local count = player:get_data('挑战层数',0) + 1
    local name = ('挑战怪LV%s'):format(count)
    if count>20 then
        return
    end

    local owner = ac.player[player.id + 8]
    local point = player.tz_point
    local angle = point / player.point1
    local unit = ac.player[16]:create_unit(name,point,angle)
    local effect = point:add_effect([[9205c7daac61cafd83c7bc648d3754d2.mdl]])
    effect:set_height(point:get_height() + 60)
    unit.effect = effect
    unit.ui_title = ('|cffff0000打我爆木材|r|n%s(|cffff0000双击挑战|r)'):format(unit.name)
    unit:set_enemy_level(unit['默认等级'])
    unit:add_restriction '免伤'
    unit:add_restriction '硬直'
    player:set_data('当前挑战怪',nil)
    unit['挑战玩家'] = player
    
    unit:event '单位-死亡'(function()
        player:set_data('挑战层数',count)
        player:notify('玩家-击败挑战怪',player,count)
        player:notify('玩家-创建挑战怪',player)
    end)

    unit:event '单位-被挑战'(function(trg,_,source)
        local time = 30
        player:set_data('当前挑战怪',unit)
        trg:remove()
        effect:remove()
        unit:remove_restriction '免伤'
        unit:remove_restriction '硬直'
        unit:set_owner(owner)
        unit:issue_order('attack',source)
        unit:loop(1000,function(t)
            if time==0 then
                t:remove()
                player:notify('玩家-取消挑战',player)
                return
            end
            unit.ui_title = ('%s(|cff00ff00%s|r)'):format(unit.name,time)
            time = time - 1
        end):on_timer()
    end)
end)


ac.game:event '玩家-选择英雄完毕'(function(_,player,hero)
    local rect1 = ac.rect.j_rect('TZ'..player.id)
    local point = rect1:get_point()
    player.tz_time = 0
    player.tz_point = point
    player:notify('玩家-创建挑战怪',player)

    hero:event '单位-发布命令'(function(_,_,id,target)
        local order = Order[id]
        if target and target['挑战玩家']==player and order=='attack' then
            target:notify('单位-被挑战',target,hero)
        end
    end)

    player:event '玩家-选择单位'(function(_,_,target)
        if target['挑战玩家']==player then
            local clock = ac.clock()
            if clock-player.tz_time<0.4*1000 then
                target:notify('单位-被挑战',target,hero)
            end
            player.tz_time = clock
        end
    end)
end)
